var prefix = jsctx + "/operatorAccount";

var operatorAccount = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#operatorAccountTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#operatorAccountTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 70;

    function load() {
        $('#operatorAccountTable').bootstrapTable({
            method: 'get',
            url: prefix + "/listPageData",
            iconSize: 'outline',
            toolbar: '#exampleToolbar',
            striped: false,
            dataType: "json",
            pagination: true,
            singleSelect: true,
            pageSize: 10,
            pageList: [10, 20, 30],
            pageNumber: 1,
            sidePagination: "server",
            paginationShowPageGo: true,
            height: h,
            cache: false,
            rightFixedColumns: false,
            showColumns: true,
            queryParams: function (params) {
                return {
                    limit: params.limit,
                    offset: (params.offset / params.limit) + 1,
                    // operatorId: $("#operatorId").val().trim(),
                    operatorName: $("#operatorName").val().trim(),
                    month: $("#month").val().trim()
                };
            },
            columns: [
                {title: '#', field: '', width: '50', align: 'left',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {field: 'operatorAccountId', title: '结算ID', align: 'left', width: '80', visible: false},
                {field: 'orderNum', title: '结算订单号', width: '120', align: 'center'},
                {field: 'operatorName', title: '加盟商', width: '160', align: 'center'},
                {field: 'amount', title: '本月结算金额', width: '100', align: 'center'},
                {field: 'status', title: '结算状态', width: '100', align: 'center',
                    formatter: function (value, row, index) {
                        if (value == '0') {
                            return '<span >待结算</span>'
                        } else if (value == '1') {
                            return '<span >已结算</span>'
                        } else if (value == '2') {
                            return '<span >结算驳回</span>'
                        } else {
                            return '<span >未知</span>'
                        }
                    }
                },
                {field: 'month', title: '所属月份', width: '200', align: 'center',
                    formatter: function (value, row, index) {
                        if (value===null){
                            return null;
                        }else{
                            return value.substring(0,10);
                        }
                    }
                },
                {
                    title: '操作',
                    field: 'roleId',
                    width: '200',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var html = '';
                        if(row.status==='0'){
                            html += '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="结算" onclick="operatorAccount.settlementOrder(\''
                                + row.operatorAccountId
                                + '\')">结算</a> ';
                        }
                        html += '<a class="btn btn-info btn-sm ' + has_edit + '" href="#" title="详情" onclick="operatorAccount.orderDetail(\''
                            + row.operatorAccountId
                            + '\')">详情</a> ';
                        return html;
                    }
                }
            ]
        });
    }

    function reLoad() {
        $(".fixed-table-header-columns").css("display", "none");
        $(".fixed-table-body-columns").css("display", "none");
        $('#operatorAccountTable').bootstrapTable('refresh');
    }

    function reset() {
        // $("#operatorId").val("");
        $("#operatorName").val("");
        $("#month").val("");
    }

    function orderDetail(brandId) {
        location.href = prefix + "/detail/"+brandId;
    }
    function settlementOrder(operatorAccountId) {
        layer.confirm('确定要结算选中的订单？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/settlement",
                type: "post",
                data: {
                    'operatorAccountId': operatorAccountId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("结算成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    /**
     * 导出加盟商结算列表
     */
    function exportOrder() {
        $.fileDownload(prefix + "/export", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                // operatorId: $("#operatorId").val().trim(),
                operatorName: $("#operatorName").val().trim(),
                month: $("#month").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    /**
     * 加载订单列表
     * @param operatorId
     */
    function loadOrderInfoData(operatorId, startTime, endTime) {
        $('#orderInfoTable').bootstrapTable({
            method: 'get',
            url: jsctx + "/orderInfo/listOrderInfo",
            iconSize: 'outline',
            toolbar: '#exampleToolbar',
            striped: false,
            dataType: "json",
            pagination: true,
            singleSelect: false,
            pageList: [10, 20, 30],
            pageNumber: 1,
            sidePagination: "server",
            paginationShowPageGo: true,
            height: h+108,
            rightFixedColumns: true,
            showColumns: true,
            queryParams: function (params) {
                return {
                    limit: params.limit,
                    offset: (params.offset / params.limit) + 1,
                    operatorId: operatorId,
                    startTime: startTime,
                    endTime: endTime
                };
            },
            columns: [
                /*{title: '#', field: '', width: '50', align: 'left',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },*/
                {field: 'outOrderNum', title: '订单编号', width: '230', align: 'left'},
                {field: 'nickname', title: '用户姓名', width: '100', align: 'left'},
                {field: 'mobile', title: '手机号', width: '100', align: 'left'},
                {field: 'carNum', title: '车牌', width: '100', align: 'left'},
                {field: 'gunNo', title: '充电枪编号', width: '120', align: 'left'},
                {field: 'accountType', title: '充电方式', width: '100', align: 'left',
                    formatter: function (value, row, index) {
                        if (value == '0') {
                            return '<span >个人充电</span>'
                        } else if (value == '1') {
                            return '<span >企业充电</span>'
                        } else if (value == '2') {
                            return '<span >共享充电</span>'
                        }
                    }
                },
                {field: 'startTime', title: '开始时间', width: '140', align: 'left'},
                {field: 'endTime', title: '结束时间', width: '140', align: 'left'},
                // {field: 'chargeDate', title: '充电日期', width: '120', align: 'left'},
                {field: 'timeStr', title: '充电时长', width: '100', align: 'left'},
                /*{field: 'nickname', title: '会员名', width: '100', align: 'left'},
                {field: 'mobile', title: '手机号', width: '100', align: 'left'},
                {field: 'beginSoc', title: '充电开始SOC', width: '100', align: 'left'},
                {field: 'soc', title: '充电结束SOC', width: '100', align: 'left'},*/
                {field: 'totalPower', title: '电量(度)', width: '80', align: 'right'},
                {field: 'totalElecMoney', title: '电费(元)', width: '80', align: 'right'},
                {field: 'totalSeviceMoney', title: '服务费(元)', width: '80', align: 'right'},
                {field: 'discountAmount', title: '优惠(元)', width: '80', align: 'right'},
                {field: 'totalMoney', title: '订单金额', width: '80', align: 'right'},
                {field: 'payAmount', title: '实付金额', width: '80', align: 'left'},
                {field: '', title: '优惠方式', width: '100', align: 'left'},
                {field: 'accountType', title: '支付方式', width: '100', align: 'left',
                    formatter: function (value, row, index) {
                        if (value == '0') {
                            return '<span >个人钱包</span>'
                        } else if (value == '1') {
                            return '<span >企业钱包</span>'
                        } else if (value == '2') {
                            return '<span >共享余额</span>'
                        }
                    }
                },
                {field: 'stationName', title: '充电站点', width: '120', align: 'left'},
                {field: 'status', title: '订单状态', width: '100', align: 'left',
                    formatter: function (value, row, index) {
                        //订单状态(0:未充电,1:充电中,2:已结束充电 3:充电结算中 4.占位费结算 5.占位中) 默认0
                        if (value == '0') {
                            return '<span >未充电</span>'
                        } else if (value == '1') {
                            return '<span >充电中</span>'
                        } else if (value == '2') {
                            return '<span >已结束充电</span>'
                        } else if (value == '3') {
                            return '<span >充电结算中</span>'
                        } else if(value == '4'){
                            return '<span >占位费结算</span>'
                        } else if(value == '5'){
                            return '<span >占位中</span>'
                        } else if(value == '6'){
                            return '<span >待结算状态</span>'
                        }
                    }
                },
                {field: 'payTime', title: '支付时间', width: '140', align: 'left'},
                // {field: 'stopMsg', title: '充电结束说明', width: '100', align: 'right'},
                {field: 'createdTime', title: '创建时间', width: '140', align: 'right'},
                {title: '操作', field: 'operate', width: '100', align: 'center', class: 'operate-td',
                    formatter: function (value, row, index) {
                        var a = '<a  class="btn btn-success btn-sm ' + has_edit + '" href="#" title="订单详情" onclick="orderInfo.orderContent(\''
                            + row.orderId
                            + '\')">订单详情</a> ';
                        return a;
                    }
                }
            ],
        });
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        reset: reset,
        settlementOrder: settlementOrder,
        orderDetail: orderDetail,
        exportOrder: exportOrder,
        loadOrderInfoData: loadOrderInfoData
    };
})();
